package roadsuggester.html;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringBufferInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;

import android.util.Log;


public class Utils
{
	//Post HTTP Request
	public static String postRequest(String postUrl, Map<String, String> postData)
	{
		try
		{
			StringBuilder rawPostData=new StringBuilder();
			for(Map.Entry<String, String> entry: postData.entrySet())
			{
				rawPostData.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
				rawPostData.append("=");
				rawPostData.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
				rawPostData.append("&");
			}

			// Send data
			URL url=new URL(postUrl);
			URLConnection conn=url.openConnection();
			conn.setDoOutput(true);
			OutputStreamWriter wr=new OutputStreamWriter(conn.getOutputStream());
			wr.write(rawPostData.toString());
			wr.flush();

			// Get the response
			BufferedReader rd=new BufferedReader(new InputStreamReader(conn.getInputStream()));

			StringBuilder response=new StringBuilder();
			String line;
			while((line=rd.readLine())!=null)
			{
				response.append(line);
			}
			wr.close();
			rd.close();
			
			String responseString=response.toString();
			Log.e("NavigationInf","AddTrack() response => "+responseString);
			return responseString;
		}
		catch(Exception e)
		{
			Log.e("NavigationInf","Error adding data to the database");
			Log.e("NavigationInf",postUrl);
			Log.e("NavigationInf",e.toString());
			
			
			return "";
		}
	}

	//Get HTTP Request
	public static String getRequest(String getUrl)
	{
		//returns the body of the response.
		try
		{
			URL url=new URL(getUrl);
			URLConnection conn=url.openConnection();
			BufferedReader rd=new BufferedReader(new InputStreamReader(conn.getInputStream()));
			StringBuilder response=new StringBuilder();
			
			String line;
			boolean headerPassed=true;
			while((line=rd.readLine())!=null)
			{
				if (headerPassed)
				{
					response.append(line);
				}
				else
				{
					if (line.equals(""))
					{
						headerPassed=true;
					}
				}
			}
			rd.close();
			
			
			return response.toString();
		}

		catch(Exception e)
		{
			return "";
		}

	}

	public static Object fromString(String s) throws IOException, ClassNotFoundException
	{
		ObjectInputStream ois=new ObjectInputStream(new StringBufferInputStream(s));
		Object o=ois.readObject();
		ois.close();
		return o;
	}

	/** Write the object to a Base64 string. */
	public static String toString(Serializable o) throws IOException
	{
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		ObjectOutputStream oos=new ObjectOutputStream(baos);
		oos.writeObject(o);
		oos.close();
		return new String(baos.toString());
	}
}
